package com.auto.signup;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestConfig {

	@Bean
	public RestTemplate sslRastTemplate() {
		PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
		connectionManager.setMaxTotal(1000);
		connectionManager.setDefaultMaxPerRoute(1000);
		HttpClientBuilder clientBuilder = HttpClientBuilder.create();
		clientBuilder.setConnectionManager(connectionManager);
		clientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(2, true));// 开启重试
		// List<E>
		// clientBuilder.setDefaultHeaders(defaultHeaders)

		HttpClient httpClient = clientBuilder.build();

		HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
		clientHttpRequestFactory.setHttpClient(httpClient);
		clientHttpRequestFactory.setConnectTimeout(5000);// Timeout
		clientHttpRequestFactory.setReadTimeout(10000);// ms

		RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
		restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
		// HttpClient httpClient = new
		return restTemplate;

		/*
		 * <list> <bean
		 * class="org.springframework.http.converter.FormHttpMessageConverter"/>
		 * <bean class=
		 * "org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter"
		 * /> <bean class=
		 * "org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"
		 * /> <bean class=
		 * "org.springframework.http.converter.StringHttpMessageConverter">
		 * <property name="supportedMediaTypes"> <list>
		 * <value>text/plain;charset=UTF-8</value> </list> </property> </bean>
		 * </list>
		 */
	}

}
